from time import sleep

from selenium.webdriver.common.by import By


class PageEcshopBackGoodsBrand():
    ''' 后台商品品牌添加 '''

    def __init__(self, driver):
        self.driver = driver
        # 元素定位器
        self.loc_goods_brand_add = (By.LINK_TEXT, "添加品牌")  # 添加商品品牌按钮
        self.loc_goods_brand_search = (By.XPATH, '/html/body/div[1]/form/input[2]')  # 搜索按钮
        self.loc_goods_brand_search_res = (By.XPATH, '//div[@id="listDiv"]/table/tbody/tr[2]/td[1]/span[2]')  # 搜索结果
        self.loc_goods_brand_del = (By.XPATH, '//div[@id="listDiv"]/table/tbody/tr[2]/td[6]/a[2]')  # 删除

        self.loc_goods_brand_brand_name = (By.NAME, "brand_name")  # 商品品牌名称
        self.loc_goods_brand_site_url = (By.NAME, "site_url")  # 商品品牌网址
        self.loc_goods_brand_brand_logo = (By.NAME, "brand_logo")  # 商品品牌图标
        self.loc_goods_brand_brand_desc = (By.NAME, "brand_desc")  # 商品品牌描述
        self.loc_goods_brand_submit = (By.XPATH, "//input[@value=' 确定 ']")  # 商品品牌描述

        self.loc_return_goods_brand_list = (By.LINK_TEXT, '返回品牌列表')

    def goods_brand_add(self):
        ''' 进入商品品牌添加页面 '''
        self.driver.find_element(*self.loc_goods_brand_add).click()

    def goods_brand_search(self):
        ''' 品牌列表搜索按钮  '''
        self.driver.find_element(*self.loc_goods_brand_search).click()

    def goods_brand_search_res(self):
        ''' 返回断言的结果--->搜索结果的第一条的品牌名 '''
        act = self.driver.find_element(*self.loc_goods_brand_search_res).text
        return act

    def goods_brand_del(self):
        ''' 点击删除 '''
        self.driver.find_element(*self.loc_goods_brand_del).click()
        self.driver.switch_to.alert.accept()

    def retrun_goods_brand_list(self):
        ''' 添加完商品品牌后 返回商品列表'''
        self.driver.find_element(*self.loc_return_goods_brand_list).click()

    def goods_brand_brand_name(self, brand_name):
        ''' 输入品牌名字 '''
        self.driver.find_element(*self.loc_goods_brand_brand_name).clear()
        self.driver.find_element(*self.loc_goods_brand_brand_name).send_keys(brand_name)

    def goods_brand_site_url(self, site_url):
        ''' 输入品牌网址 '''
        self.driver.find_element(*self.loc_goods_brand_site_url).send_keys(site_url)

    def goods_brand_brand_logo(self, brand_logo):
        ''' 输入用户名 '''
        self.driver.find_element(*self.loc_goods_brand_brand_logo).send_keys(brand_logo)

    def goods_brand_brand_desc(self, brand_desc):
        ''' 输入用户名 '''
        self.driver.find_element(*self.loc_goods_brand_brand_desc).send_keys(brand_desc)

    def goods_brand_submit(self):
        ''' 输入用户名 '''
        self.driver.find_element(*self.loc_goods_brand_submit).click()

    def brand_add(self, brand_name):
        self.goods_brand_add()
        sleep(1)
        self.goods_brand_brand_name(brand_name)
        self.goods_brand_submit()
        self.retrun_goods_brand_list()

    def brand_find(self, brand_name):
        self.goods_brand_brand_name(brand_name)
        self.goods_brand_search()

    def brand_assert(self):
        return self.goods_brand_search_res()

    def brand_del(self):
        self.goods_brand_del()
